<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_user_class extends AWS_CONTROLLER
{

	function get_permission_action()
	{
	
	}

	public function setup()
	{
		admin_session_class::check_admin_login($this->get_permission_action());
		$this->crumb("会员管理", "admin/user/");
	}

	public function index_action()
	{
		$this->list_action();
	}

	public function list_action()
	{
		if ($_POST['keyword'])
		{
			header('Location: ' . $_SERVER['REQUEST_URI'] . (preg_match('/\/$/', $_SERVER['REQUEST_URI']) ? '' : '__') . 'keyword-' . $_POST['keyword']);
		}
		
		$per_page = 15;
		
		$page_id = $_GET['page'];
		
		if (empty($page_id))
		{
			$page_id = 1;
		}
		
		$limit = ($page_id - 1) * $per_page . "," . $per_page;
		
		$keyword = $_GET['keyword'];
		
		$page_url = 'admin/user/list/';
		
		$m_keyword = mysql_escape_string($keyword);
		
		if (! empty($keyword))
		{
			$user_list = $this->model('user_manage')->get_user_list(false, "user_name LIKE '%" . $m_keyword . "%' OR email LIKE '%" . $m_keyword . "%'", $limit);
			$totalnum = $this->model('user_manage')->get_user_list(true, "user_name LIKE '%" . $m_keyword . "%' OR email LIKE '%" . $m_keyword . "%'");
			$keyword_url .= "keyword-" . $keyword;
		}
		else
		{
			$user_list = $this->model('user_manage')->get_user_list(false, "", $limit);
			$totalnum = $this->model('user_manage')->get_user_list(true);
		}
		
		$this->model('pagination')->initialize(array(
			'base_url' => $page_url . $keyword_url, 
			'total_rows' => $totalnum, 
			'per_page' => $per_page, 
			'last_link' => '末页', 
			'first_link' => '首页', 
			'next_link' => '下一页 »', 
			'prev_link' => '« 上一页', 
			'anchor_class' => ' class="number"', 
			'cur_tag_open' => '<a class="number current">', 
			'cur_tag_close' => '</a>'
		));
		
		TPL::assign('pagination', $this->model('pagination')->create_links());
		TPL::assign('system_groups', $this->model('user_manage')->get_group('type = 0'));
		TPL::assign('keyword', $keyword);
		TPL::assign('list', $user_list);
		TPL::output('admin/user_list', true, false);
	}

	public function group_list_action()
	{
		$system_group = $this->model('user_manage')->get_group('type = 0');
		
		$mem_group = $this->model('user_manage')->get_group('type = 1');
		
		$this->crumb("用户组管理", "admin/user/group_list");
		
		TPL::assign('mem_group', $mem_group);
		
		TPL::assign('system_group', $system_group);
		
		TPL::output('admin/user_group_list', true, false);
	}

	public function group_save_ajax()
	{
		define('IN_AJAX', TRUE);
		
		if ($group_data = $_POST['group'])
		{
			foreach ($group_data as $key => $val)
			{
				if (empty($val['group_name']))
				{
					H::ajax_json_output(AWS_APP::RSM(null, "-1", "用户组名称不能为空"));
				}
				
				if($val['reputation_factor'])
				{
					$val['reputation_factor'] = intval($val['reputation_factor']);
				}
				
				$this->model('user_manage')->update_group('group_id = ' . $key, $val);
			}
		}
		
		if($group_new = $_POST['group_new'])
		{
			foreach($group_new['group_name'] as $key => $val)
			{
				if(trim($group_new['group_name'][$key]))
				{
					$this->model('user_manage')->add_group($group_new['group_name'][$key], $group_new['reputation_lower'][$key], $group_new['reputation_higer'][$key], $group_new['reputation_factor'][$key]);
				}
			}
		}
		
		if($group_ids = $_POST['group_ids'])
		{
			foreach($group_ids as $key => $val)
			{
				$group_info = $this->model('account')->get_group_by_id($val);
				
				if($group_info['type'] == 1)
				{
					$this->model('user_manage')->remove_group($val);
				}
				else
				{
					H::ajax_json_output(AWS_APP::RSM(null, "-1", "系统用户组不可删除。"));
				}
			}
		}

		if($group_new || $group_ids)
		{
			$rsm_array = array(
				'url' => get_js_url('/admin/user/group_list/'),
			);
		}
		
		H::ajax_json_output(AWS_APP::RSM($rsm_array, "1", "用户组更新成功"));
	}

	public function group_edit_action()
	{
		$group_id = intval($_GET['group_id']);
		
		$group = $this->model('account')->get_group_by_id($group_id);
		
		if (! $group)
		{
			H::js_pop_msg('用户组不存在');
		}
		
		$this->crumb("用户组编辑", "admin/user/group_list");
		
		TPL::assign('group', $group);
		
		TPL::assign('group_pms', $group['permission']);
		
		TPL::output('admin/user_group_edit', true, false);
	}

	/**
	 * 保存用户组编辑
	 */
	public function group_edit_process_action()
	{
		$group_id = intval($_GET['group_id']);
		
		$permission_array = array(
			'is_administortar', 
			'is_moderator', 
			'publish_question', 
			'edit_question', 
			'edit_topic', 
			'upload_attach', 
			'publish_url', 
			'human_valid', 
			'question_valid_hour', 
			'answer_valid_hour'
		);
		
		$group_setting = array();
		
		foreach ($permission_array as $permission)
		{
			$group_setting[$permission] = $_POST[$permission];
		}
		
		$retval = $this->model('user_manage')->update_group('group_id = ' . $group_id, array(
			'permission' => serialize($group_setting)
		));
		
		H::ajax_json_output(AWS_APP::RSM(null, "1", "用户组更新成功"));
	}

	/**
	 * 修改用户资料
	 * Enter description here ...
	 */
	public function edit_action()
	{
		$user_id = intval($_GET['uid']);
		
		$this->crumb("修改用户", "admin/user/edit/uid-" . $user_id);
		
		$user = $this->model('account')->get_users_by_uid($user_id);
		
		$system_group = $this->model('user_manage')->get_group('type = 0');
		
		TPL::assign('system_group', $system_group);
		TPL::assign('user', $user);
		TPL::output("admin/user_edit", true, false);
	}

	/**
	 * 用户修改处理
	 * Enter description here ...
	 */
	public function user_edit_ajax_action()
	{
		define('IN_AJAX', TRUE);
		
		$user_id = intval($_POST['uid']);
		
		if ($user_id == 0)
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "系统错误，用户ID为空"));
		}
		
		$update_user = $this->model('user_manage')->user_field_filter($_POST);
		
		$update_user = $this->model('user_manage')->format_user_field($_POST);
		
		if ($this->user_info['group_id'] != '1')
		{
			unset($update_user['group_id']);
		}
		
		if ($_POST['delete_avatar'])
		{
			$this->model('account')->delete_avatar($user_id);
		}
		
		if (! empty($update_user['password']))
		{
			$this->model('account')->update_user_password_ingore_oldpassword($update_user['password'], $user_id, fetch_salt(4));
		}
		
		unset($update_user['uid']);
		unset($update_user['password']);
		
		$this->model('account')->update_users_fields($update_user, $user_id);
		
		H::ajax_json_output(AWS_APP::RSM(null, "1", "用户资料修改成功"));
	}

	/**
	 * 设置会员状态
	 * Enter description here ...
	 */
	public function forbidden_status_ajax_action()
	{
		define('IN_AJAX', TRUE);
		
		$user_id = $_GET['user_id'];
		
		$status = $_GET['status'];
		
		if (! in_array($status, array(
			'0', 
			'1'
		)))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "系统错误"));
		}
		
		$retval = $this->model('account')->update_users_fields(array(
			'forbidden' => $status
		), $user_id);
		
		if ($retval)
		{
			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/admin/user/list/')
			), "1", ""));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(), "-1", "添加失败，请联系管理员"));
		}
	}

	/**
	 * 列出在线列表
	 * Enter description here ...
	 */
	public function online_list_action()
	{
		$per_page = 20;
		
		$page = $_GET['page'];
		
		if (intval($page) <= 0)
		{
			$page = 1;
		}
		
		$limit = ($page - 1) * $per_page . "," . $per_page;
		
		$page_url = 'admin/user/online_list/';
		
		$r_time = time() - (get_setting('online_interval') * 60);
		
		$online_users = $this->model('online')->get_db_online_users(false, "last_active >= " . $r_time, $limit);
		
		$total_count = $this->model('online')->get_db_online_users(true, "last_active >= " . $r_time);
		
		$online_users = $this->online_users_format($online_users);
		
		$this->model('pagination')->initialize(array(
			'base_url' => $page_url, 
			'total_rows' => $total_count, 
			'per_page' => $per_page, 
			'last_link' => '末页', 
			'first_link' => '首页', 
			'next_link' => '下一页 »', 
			'prev_link' => '« 上一页', 
			'anchor_class' => ' class="number"', 
			'cur_tag_open' => '<a class="number current">', 
			'cur_tag_close' => '</a>'
		));
		
		TPL::assign('pagination', $this->model('pagination')->create_links());
		
		TPL::assign('list', $online_users);
		
		TPL::assign('total_count', $total_count);
		
		TPL::output('admin/user_online');
	}

	/**
	 * 在线会员列表格式化
	 * Enter description here ...
	 * @param unknown_type $online_users
	 */
	public function online_users_format($online_users)
	{
		if (is_array($online_users))
		{
			foreach ($online_users as $user)
			{
				$uids[] = $user['uid'];
			}
		}
		if (empty($uids))
		{
			return $online_users;
		}
		
		$uids = array_unique($uids);
		
		if ($_user_infos = $this->model('account')->get_users_by_uids($uids))
		{
			$user_infos = array();
			
			foreach ($_user_infos as $user)
			{
				$user_infos[$user['uid']] = $user;
			}
		}
		foreach ($online_users as $key => $val)
		{
			$online_users[$key]['userinfo'] = $user_infos[$val['uid']];
		}
		
		return $online_users;
	}

}